package com.saicent.note.core;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.saicent.support.model.SimpleModelSupport;
import com.saicent.support.utils.StringUtils;

public class Article extends SimpleModelSupport<String> {

	private String title;
	private List<String> categories = new ArrayList<>();
	private String location;
	private String content;

	public static Article initArticle(
			String title, String categories, String location) {
		if (!StringUtils.isNoneBlank(title, categories, location)) {
			return null;
		}
		String[] categoryArray = categories.split(",");
		return new Article(title, Arrays.asList(categoryArray), null, location);
	}
	
	public static Article createArticle(
			String title, String categories, String content) {
		if (!StringUtils.isNoneBlank(title, categories, content)) {
			return null;
		}
		String[] categoryArray = categories.split(",");
		return new Article(title, Arrays.asList(categoryArray), content, null);
	}
	
	private Article(
		String title, List<String> categories, String content, String location) {
		for (int i = 0; i < categories.size(); i++) {
			categories.set(i, categories.get(i).trim());
		}
		this.title = title;
		this.location = location;
		this.categories = categories;
		this.content = content;
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List<String> getCategories() {
		return categories;
	}

	public void setCategories(List<String> categories) {
		this.categories = categories;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Article) {
			Article article = (Article) obj;
			if (this.location.equals(article.location)) {
				return true;
			}
			return false;
		} else {
			return false;
		}
	}
	
	@Override
	public int hashCode() {
		return this.location.hashCode();
	}
}
